package com.ocom.webapi.controller;

import com.ocom.common.constant.AccessServiceUrl;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.security.authentication.YoCiUser;
import com.ocom.security.utils.SecurityUtils;
import com.ocom.webapi.service.AccessAccountService;
import com.ocom.webapi.vo.accessaccount.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@PreAuthorize("@pms.hasRole('ROLE_WEB')" )
public class AccessAccountController {

    @Autowired
    private AccessAccountService accessAccountService;
    /**
     * 账号列表（AccessAccount/onShow）
     * */
    @PostMapping(AccessServiceUrl.URL_AccessAccountOnShow)
    public Result AccessAccountOnShow(@Validated @RequestBody OnShowVo onShowVo){
        YoCiUser yoCiUser= SecurityUtils.getYoCiUser();
        onShowVo.setAgentId(yoCiUser.getComId());
//        if (onShowVo.getComId() == null){
//            log.error("单位编号为空");
//            return ResultUtil.error(ResultEnum.Err_203,"单位编号为空");
//        }
        if (onShowVo.getPage() == null){
            log.error("页码page为空");
            return ResultUtil.error(ResultEnum.Err_203,"页码page为空");
        }
        if (onShowVo.getSize() == null){
            log.error("页数size为空");
            return ResultUtil.error(ResultEnum.Err_203,"页数size为空");
        }
        return accessAccountService.AccessAccountOnShow(onShowVo);
    }


    /**
     * 账号修改（AccessAccount/modify）
     * */
    @PostMapping(AccessServiceUrl.URL_AccessAccountModify)
    public Result AccessAccountModify(@Validated @RequestBody ModifyVo modifyVo){
        if (modifyVo.getComId() == null){
            log.error("单位编号为空");
            return ResultUtil.error(ResultEnum.Err_203,"单位编号为空");
        }
        YoCiUser yoCiUser = SecurityUtils.getYoCiUser();
        modifyVo.setComId(yoCiUser.getComId());
        if (modifyVo.getId() == null){
            log.error("设备账号ID为空");
            return ResultUtil.error(ResultEnum.Err_203,"设备账号ID为空");
        }
        if (modifyVo.getDeviceNo() == null){
            log.error("设备编号为空");
            return ResultUtil.error(ResultEnum.Err_203,"设备编号为空");
        }
        return accessAccountService.AccessAccountModify(modifyVo);
    }

    /**
     * 账号删除（AccessAccount/delete）
     * */
    @PostMapping(AccessServiceUrl.URL_AccessAccountDelete)
    public Result AccessAccountDelete(@Validated @RequestBody DeleteVo deleteVo){
        if (deleteVo.getComId() == null){
            log.error("单位编号为空");
            return ResultUtil.error(ResultEnum.Err_203,"单位编号为空");
        }
        YoCiUser yoCiUser = SecurityUtils.getYoCiUser();
        deleteVo.setComId(yoCiUser.getComId());
        if (deleteVo.getIds().isEmpty()){
            log.error("配置信息删除ids为空");
            return ResultUtil.error(ResultEnum.Err_203,"配置信息删除ids为空");
        }
        return accessAccountService.AccessAccountDelete(deleteVo.getComId(),deleteVo.getIds());
    }

    /**
     *     账号新增（AccessAccount/add）
     */
    @PostMapping(AccessServiceUrl.URL_AccessAccountAdd)
    public Result AccessAccountAdd(@Validated @RequestBody AddVo addVo){
        if (addVo.getComId() == null){
            log.error("单位编号为空");
            return ResultUtil.error(ResultEnum.Err_203,"单位编号为空");
        }
        YoCiUser yoCiUser = SecurityUtils.getYoCiUser();
        addVo.setComId(yoCiUser.getComId());
        if (addVo.getLoginType() == null){
            log.error("登录类型为空");
            return ResultUtil.error(ResultEnum.Err_203,"设备登录类型为空");
        }
        if (addVo.getDeviceNo() == null){
            log.error("设备编号为空");
            return ResultUtil.error(ResultEnum.Err_203,"设备编号为空");
        }
        if (addVo.getAccount() == null){
            log.error("登录账号为空");
            return ResultUtil.error(ResultEnum.Err_203,"登录账号为空");
        }

        return accessAccountService.AccessAccountAdd(addVo);
    }

    /**
     * 重置账号权限范围(resetMngUserScope)
     * */
    @PostMapping(AccessServiceUrl.URL_ResetMngUserScope)
    public Result ResetMngUserScope(@Validated @RequestBody ResetMngUserScopeVo resetMngUserScopeVo){
        return accessAccountService.ResetMngUserScope(resetMngUserScopeVo);
    }

}
